﻿using NFine.Code;
using NFine.Data;
using NFine.Domain.ViewModel.VDto;
using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.BusinessManage;
using NFine.Domain.ViewModel.QueryDto;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Repository.BusinessManage
{
    public class D_SalaryHistoryRepository : RepositoryBase<D_SalaryHistoryEntity>, ID_SalaryHistoryRepository
    {

        public List<D_SalaryHistoryDto> GetSalaryHistoryList(Pagination pagination, SearchInputDto searchInput)
        {
            var query = from charge in dbcontext.Set<D_SalaryHistoryEntity>()
                               .WhereIf(!string.IsNullOrEmpty(searchInput.ChangeType), o => o.ChangeType == searchInput.ChangeType)
                               .WhereIf(searchInput.TMs.HasValue,o=>o.TriggerTime >= searchInput.TMs.Value)
                               .WhereIf(searchInput.TMe.HasValue, o => o.TriggerTime <= searchInput.TMe.Value)
                        join u1 in dbcontext.Set<UserEntity>()
                            .WhereIf(!string.IsNullOrEmpty(searchInput.Account), o => o.F_Account.Contains(searchInput.Account))
                            .WhereIf(!string.IsNullOrEmpty(searchInput.AccountName), o => o.F_RealName.Contains(searchInput.AccountName)) on charge.UserID equals u1.F_Id
                        join u2 in dbcontext.Set<UserEntity>() on charge.TriggerUserID equals u2.F_Id
                        orderby charge.TriggerTime descending
                        select new D_SalaryHistoryDto
                        {
                            ID = charge.ID,
                            AccountType = charge.AccountType,
                            ChangeAmount = charge.ChangeAmount,
                            ChangeType = charge.ChangeType,
                            Remark = charge.Remark,
                            UserID = charge.UserID,
                            TriggerTime = charge.TriggerTime,
                            TriggerUserID = charge.TriggerUserID,
                            Account = u1.F_Account,
                            AccountName = u1.F_RealName,
                            TAccount = u2.F_Account,
                            TAccountName = u2.F_RealName
                        };
            pagination.records = query.Count();
            int start = pagination.rows * (pagination.page - 1);
            if (start > pagination.records)
            {
                start = 0;
                pagination.page = 1;
            }
            var list = query.Skip(start).Take(pagination.rows).ToList();
            return list;
        }
    }
}
